package com.xiguthings.waka.service.useranswer.impl;

import com.xiguthings.waka.dao.DataBaseStatus;
import com.xiguthings.waka.dao.mapper.UserAnswerMapper;
import com.xiguthings.waka.dao.mapper.UserMapper;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.User;
import com.xiguthings.waka.po.UserAnswer;
import com.xiguthings.waka.po.UserAnswerExample;
import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.service.useranswer.IUserAnswerService;
import com.xiguthings.waka.web.controller.base.BaseVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserAnswerServiceImpl implements IUserAnswerService {

    @Resource
    private UserAnswerMapper userAnswerMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int countByExample(UserAnswerExample userAnswerExample) throws Exception {
        // TODO
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int deleteByExample(UserAnswerExample userAnswerExample) throws Exception {
        // TODO
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int deleteByPrimaryKey(String id) throws Exception {
        // TODO
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insert(UserAnswer userAnswer) throws Exception {
        if (userAnswer == null || StringUtils.isBlank(userAnswer.getUserId())) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "必填参数为空");
        }
        String id = StringUtils.getRandomUUID();
        userAnswer.setUserAnswerId(id);
        Date now = new Date();
        userAnswer.setSubmitTime(now);
        userAnswer.setStatus(DataBaseStatus.NORMAL);

        // 修改用户的冷却时间
        User user = userMapper.selectByPrimaryKey(userAnswer.getUserId());
        user.setCdBeginTime(now);
        userMapper.updateByPrimaryKeySelective(user);
        return userAnswerMapper.insert(userAnswer);//插入新数据
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insertSelective(UserAnswer userAnswer) throws Exception {
        // TODO
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<UserAnswer> selectByExample(UserAnswerExample userAnswerExample) throws Exception {

        return userAnswerMapper.selectByExample(userAnswerExample);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public UserAnswer selectByPrimaryKey(String id) throws Exception {

        return userAnswerMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByExampleSelective(UserAnswer userAnswer, UserAnswerExample userAnswerExample) throws Exception {
        // TODO
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByExample(UserAnswer userAnswer, UserAnswerExample userAnswerExample) throws Exception {
        // TODO
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(UserAnswer userAnswer) throws Exception {
        // TODO
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByPrimaryKey(UserAnswer userAnswer) throws Exception {
        // TODO
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page selectByPage(Page pageEntity, UserAnswerExample userAnswerExample) throws Exception {
        // TODO
        return null;
    }
}
